package qj.tool.config;

import java.math.BigDecimal;
import java.util.Date;

import qj.util.DateUtil;

public class Config {

	public String name;

	public String string;
	
	public Boolean bool;
	
	public Date date;
	
	public Integer integer;
	
	public Double doubl;
	
	public BigDecimal decimal;
	
	public String type;

	public Config() {
	}

	public Config(String name, String string) {
		this.name = name;
		this.string = string;
		this.type = "String";
	}

	public Config(String name, boolean bool) {
		this.name = name;
		this.bool = bool;
		this.type = "Boolean";
	}

	public Config(String name, Date date) {
		this.name = name;
		this.date = date;
		this.type = "Date";
	}

	public Config(String name, Integer integer) {
		this.name = name;
		this.integer = integer;
		this.type = "Integer";
	}

	public Config(String name, Double doubl) {
		this.name = name;
		this.doubl = doubl;
		this.type = "Double";
	}
	public Config(String name, BigDecimal decimal) {
		this.name = name;
		this.decimal = decimal;
		this.type = "Decimal";
	}
	public String getStringValue() {
		if ("Boolean".equals(type)) {
			return "" + bool;
		} else if ("Date".equals(type)) {
			return "" + date;
		} else if ("Integer".equals(type)) {
			return "" + integer;
		} else if ("Double".equals(type)) {
			return "" + doubl;
		} else if ("Decimal".equals(type)) {
			return "" + decimal;
		} else {
			return string;
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((bool == null) ? 0 : bool.hashCode());
		result = prime * result + ((date == null) ? 0 : date.hashCode());
		result = prime * result + ((doubl == null) ? 0 : doubl.hashCode());
		result = prime * result + ((decimal == null) ? 0 : decimal.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((integer == null) ? 0 : integer.hashCode());
		result = prime * result + ((string == null) ? 0 : string.hashCode());
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		return result;
	}

	public static Config create(String id, String value, String type) {
		Config config = new Config();
		config.name = id;
		config.type = type;
		if ("String".equals(type)) {
			config.string = value;
		} else if ("Boolean".equals(type)) {
			config.bool = Boolean.valueOf(value);
		} else if ("Date".equals(type)) {
			config.date = DateUtil.parse(value);
		} else if ("Integer".equals(type)) {
			config.integer = Integer.valueOf(value);
		} else if ("Double".equals(type)) {
			config.doubl = Double.valueOf(value);
		} else if ("Decimal".equals(type)) {
			config.decimal = new BigDecimal(value);
		} else {
			throw new RuntimeException("Unknown type: " + type);
		}
		return config;
	}
}
